<?php
require_once("pre.php");
require_once("auth.php");

if(! est_autorise("acces_configuration")) exit;

/* -- Traitement des actions ---------------------------------- */

$message_erreur = false;

try {

	if (isset($action))
	{
		switch($action){
			case "supprimer":
				ActionsAdminModules::instance()->supprimer($nom);
			break;

			case 'activer':
				ActionsAdminModules::instance()->activer($nom);
			break;

			case 'desactiver':
				ActionsAdminModules::instance()->desactiver($nom);
			break;

			case 'ajouter' :
				if(isset($_FILES['plugin'])) {
					if ($_FILES['plugin']['error'] == UPLOAD_ERR_OK) {
						$plugin = $_FILES['plugin']['tmp_name'];
						$plugin_name = $_FILES['plugin']['name'];

						ActionsAdminModules::instance()->installer($plugin, $plugin_name);
					} else {
						throw new TheliaException(trad("L'envoi du fichier a échoué"), TheliaException::MODULE_ECHEC_UPLOAD);
					}
				}
			break;
		}

		redirige($_SERVER['PHP_SELF']);
	}

	// Mise a jour de la base suivant le contenu du repertoire plugins
	ActionsAdminModules::instance()->mettre_a_jour();

} catch (Exception $ex) {
	$message_erreur = $ex->getMessage() . ' (erreur '.$ex->getCode().')';

}

function afficher_liste_plugins($type, $label) {

	?>
	<tr>
		<th class="entete_configuration" colspan="4"><?php echo $label ?></th>
	</tr>
	<?php
	$liste = ActionsAdminModules::instance()->lister($type);

	$fond = 'fonce';

	foreach($liste as $plugin) {

		$titre = ActionsAdminModules::instance()->lire_titre_module($plugin);

		$description = $plugin->xml->description;

		if (empty($description))  $description = trad('Description non disponible');

		?>
		<tr class="<?php echo $fond ?><?php if ($plugin->actif != 1) echo " plugininactif" ?>">

			<td class="titre"><?php echo $titre; ?><?php echo $plugin->xml->version != '' ? " v".$plugin->xml->version : ''?></td>

			<td rowspan="2" style="width: 50px;">
				<?php if ($plugin->activable && $plugin->type != Modules::FILTRE) { ?>
					<a href="plugins_modifier.php?nom=<?php echo $plugin->nom ?>&actif=0" class="txt_vert_11"><?php echo trad('Editer'); ?></a>&nbsp;
				<?php } ?>
			</td>

			<td rowspan="2" style="width: 50px;">
				<?php if ($plugin->actif) { ?>
					<a href="plugins.php?action=desactiver&nom=<?php echo $plugin->nom ?>&actif=0" class="txt_vert_11"><?php echo trad('Desactiver'); ?></a>&nbsp;
				<?php } else if ($plugin->activable) { ?>
					<a href="plugins.php?action=activer&nom=<?php echo $plugin->nom ?>&actif=1" class="txt_vert_11"><?php echo trad('Activer'); ?></a>&nbsp;
				<?php } else { ?>
					<span style="color: #f00; font-weight: bold" ?><?php echo trad('Incompatible'); ?></span>&nbsp;
				<?php } ?>
			</td>

			<td rowspan="2" style="width: 30px; text-align: center;">
				<a href="plugins.php?action=supprimer&nom=<?php echo $plugin->nom; ?>" title="<?php echo trad("Supprimer ce plugin"); ?>" onclick="return supprimer('<?php echo str_replace("'", "\\'", $titre); ?>');"><img src="gfx/supprimer.gif" width="9" height="9" border="0" /></a>
			</td>
		</tr>

		<tr class="<?php echo $fond ?> modules_info<?php if ($plugin->actif != 1) echo " plugininactif" ?>">
			<td><span style="font-size: 0.9em"><?php echo $description ?></span></td>
		</tr>
		<?php

		$fond = ($fond == 'fonce') ? 'claire' : 'fonce';
	}

	if (empty($liste))
	{
		?>
		<tr class="claire">
			<td colspan="4"><?php echo trad('Aucunplugin'); ?></td>
		</tr>
		<?php
	}

	?>
	<tr>
		<td colspan="4" class="separateur"></td>
	</tr>
	<?php
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php require_once("title.php");?>

	<script type="text/javascript">
		function supprimer(nom) {
			return confirm("<?php echo trad("Voulez-vous supprimer définitivement le plugin ") ?>" + nom + " ?");
		}
	</script>
</head>

<body>
<div id="wrapper">
<div id="subwrapper">

<?php
	$menu="configuration";
	require_once("entete.php");
?>

<div id="contenu_int">
   <p align="left">
	   	<a href="accueil.php" class="lien04"><?php echo trad('Accueil'); ?> </a>
	   	<img src="gfx/suivant.gif" width="12" height="9" border="0" />
	   	<a href="configuration.php" class="lien04"><?php echo trad('Configuration'); ?></a>
	   	<img src="gfx/suivant.gif" width="12" height="9" border="0" />
	   	<a href="plugins.php" class="lien04"><?php echo trad('Gestion_plugins'); ?></a>
    </p>

	<div id="bloc_informations">
		<ul style="width:956px; margin-bottom:10px; background-color:red">
			<li class="entete_configuration" style="width:451px"><?php echo trad('AJOUTER_PLUGIN'); ?></li>
			<li class="entete_configuration" style="padding:4px 0 5px 0; width:500px" >
				<div class="fonction_ajout" style="padding-top:-10px; margin-right: 5px;">
					<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
						<input type="hidden" name="action" value="ajouter" />
		                <input type="file" name="plugin" class="form" />
		               	<input type="submit" value="<?php echo trad('Valider'); ?>" />
					</form>
				</div>
			</li>
		</ul>

	    <?php if ($message_erreur !== false) { ?>
		    <ul style="width:956px; margin-bottom:10px; background-color:red">
		    <li style="padding: 5px; height: auto; width:100%; font-weight: bold; color: #fff;"><?php echo $message_erreur; ?></li>
		    </ul>
	    <?php } ?>

		<table class="modules">

		    <?php afficher_liste_plugins(Modules::CLASSIQUE, trad('LISTE_PLUGINS_CLASSIQUES')); ?>
	        <?php afficher_liste_plugins(Modules::PAIEMENT, trad('LISTE_PLUGINS_PAIEMENTS')); ?>
	        <?php afficher_liste_plugins(Modules::TRANSPORT, trad('LISTE_PLUGINS_TRANSPORTS')); ?>
	        <?php afficher_liste_plugins(Modules::FILTRE, trad('LISTE_FILTRE')); ?>

		</table>
	</div>

</div>
<?php require_once("pied.php");?>
</div>
</div>
</body>
</html>
